﻿using Jianshu_Research_Tools;
using System;
using System.ComponentModel;
using System.Threading;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using XianShi;

namespace JRTSharp_Test
{
    class Program
    {
        public static int comments = 160;
        public static float[] data = new float[comments + 1];
         
        static void Main(string[] args)
        {
            var door = BasicApis.GetArticleJsonDataApi("https://www.jianshu.com/p/ed2f20d40676");
            Console.WriteLine(door);
            Console.ReadKey();
        }

        /// <summary>
        /// 提供的测试函数：粉丝黏度计算工具。使用时请将入口点函数标记为STAThread(单一线程单元线程)特性。
        /// </summary>
        [STAThread]
        static void GetViscosity()
        {
            Console.WriteLine("方法盒子工作室(R)简书粉丝黏性可视化工具。");
            Console.WriteLine("Model:Static_Line Started Success.");
            Console.Write("请输入文章网址：");
            string url = Console.ReadLine();
            Console.Write("请输入文章楼数：");
            string floors = Console.ReadLine();
            comments = Int(floors);
            Console.WriteLine("分析小时数：72Hr.");
            Console.WriteLine("开始获取数据。");
            string articleJson = BasicApis.GetArticleJsonDataApi(url);
            JsonStruct.Asimov.Root articleInfo = ArticleAparser(articleJson);
            int articleId = articleInfo.id;
            string articlePublishJson = articleInfo.first_shared_at;
            int articleCommentCount = articleInfo.public_comment_count;
            string articlePublishDate = articlePublishJson.Split('T')[0];
            string articlePublishTime = articlePublishJson.Split('T')[1].Split('+')[0];
            DateTime dt = new DateTime(
                Int(articlePublishDate.Split('-')[0]),
                Int(articlePublishDate.Split('-')[1]),
                Int(articlePublishDate.Split('-')[2]),
                Int(articlePublishTime.Split(':')[0]),
                Int(articlePublishTime.Split(':')[1]),
                Int(articlePublishTime.Split(':')[2])
            );

            string commentJson = BasicApis.GetArticleCommentsJsonDataApi(articleId, 1, comments);
            JavaScriptSerializer aparser = new JavaScriptSerializer();
            JsonStruct.JianShuComments.Root infoList = aparser.Deserialize<JsonStruct.JianShuComments.Root>(commentJson);
            int count = default(int);
            foreach (var item in infoList.comments)
            {
                ++count;
                string commentPublishDateTime = item.created_at;
                //2021-11-02T10:48:44.000+08:00 -> 2021/11/02 10:48:44
                commentPublishDateTime = commentPublishDateTime.Split('+')[0];
                commentPublishDateTime = commentPublishDateTime.Split('.')[0];
                commentPublishDateTime = commentPublishDateTime.Replace('-', '/');
                commentPublishDateTime = commentPublishDateTime.Replace('T', ' ');
                DateTime dv = new DateTime(
                    Int(commentPublishDateTime.Split(' ')[0].Split('/')[0]),
                    Int(commentPublishDateTime.Split(' ')[0].Split('/')[1]),
                    Int(commentPublishDateTime.Split(' ')[0].Split('/')[2]),
                    Int(commentPublishDateTime.Split(' ')[1].Split(':')[0]),
                    Int(commentPublishDateTime.Split(' ')[1].Split(':')[1]),
                    Int(commentPublishDateTime.Split(' ')[1].Split(':')[2])
                    );
                var timeStamp = (dv - dt).TotalHours;
                Console.WriteLine(timeStamp);
                data[count] = (float)timeStamp;
                //data[0] = data[60];
            }

            //UI ui = new UI(new float[]{ 95, 30, 20, 23, 60, 87, 42, 77, 92, 51, 29 });
            //ui.Show();

            Application.Run(new UI(data));

            Console.ReadKey();

        }

        static JsonStruct.Asimov.Root ArticleAparser(string jsonResult)
        {
            JavaScriptSerializer aparser = new JavaScriptSerializer();
            JsonStruct.Asimov.Root infoList = aparser.Deserialize<JsonStruct.Asimov.Root>(jsonResult);
            return infoList;
        }

        static JsonStruct.JianShuComments.Root CommentAparser(string jsonResult)
        {
            JavaScriptSerializer aparser = new JavaScriptSerializer();
            JsonStruct.JianShuComments.Root infoList = aparser.Deserialize<JsonStruct.JianShuComments.Root>(jsonResult);
            return infoList;
        }


        static int Int(string str)
        {
            try
            {
                return System.Convert.ToInt32(str);
            }
            catch
            {
                return System.Convert.ToInt32(str.Split('.')[0]);
            }
        }
    }
}
